#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include "stack.h"
#include "../malloc/malloc.h"

#define SD_STACK_INIT_SIZE 32

struct __sd_stack {
    size_t max;
    size_t sp;
    size_t size;
    size_t iter;
    void **array;
};

/******************************************************************************/
sd_stack_t* sd_stack_new(size_t max)
{
    sd_stack_t* this;

    this        = (sd_stack_t*)sd_calloc(1, sizeof(sd_stack_t));
    this->max   = max == 0 ? INT_MAX : max;
    this->size  = SD_STACK_INIT_SIZE;
    this->sp    = 0;
    this->array = (void **)sd_calloc(this->size, sizeof(*this->array));

    return this;
}

/******************************************************************************/
void sd_stack_delete(sd_stack_t* this, void (*free_data_fn)(void *))
{
    if (!this)
	return;

    sd_stack_clear(this, free_data_fn);

    PON_FREE_FUNCTION(this->array);
    PON_FREE_FUNCTION(this);
}

/******************************************************************************/
size_t sd_stack_get_nelem(const sd_stack_t* this)
{
    return this ? this->sp : -1;
}

/******************************************************************************/
void sd_stack_clear(sd_stack_t* this, void (*free_data_fn)(void *))
{
    if (!this)
	return;

    if (free_data_fn) {
	while (this->sp > 0) {
	    free_data_fn(this->array[--(this->sp)]);
	}
    }
}

/******************************************************************************/
void* sd_stack_begin(sd_stack_t* this)
{
    if (!this)
	return NULL;

    this->iter = 0;
    return this->array[this->iter];
}

/******************************************************************************/
void* sd_stack_next(sd_stack_t* this)
{
    if (this && this->iter < this->sp) 
	return this->array[this->iter++];

    return NULL;
}

/******************************************************************************/
void* sd_stack_end(sd_stack_t* this)
{
    return sd_stack_peek(this);
}

/******************************************************************************/
void* sd_stack_peek(sd_stack_t* this)
{
    if (!this || !this->sp) 
	return NULL;

    return this->array[this->sp - 1];
}

/******************************************************************************/
int sd_stack_push(sd_stack_t* this, void *data)
{
    if (this == NULL)
	return -1;

    if (this->sp == this->size) {
	size_t new_size;

	if (this->size == this->max)
	    return -1;

	if (this->size * 2 > this->max) {
	    new_size = this->max;
	} else {
	    new_size = this->size * 2;
	}
	this->size = new_size;
	this->array = (void **)sd_realloc(this->array, sizeof(*this->array) * this->size);
    }

    assert(this->sp <= this->size);

    this->array[this->sp++] = data;
    return 0;
}

/******************************************************************************/
void* sd_stack_pop(sd_stack_t* this)
{
    if (this == NULL || this->sp == 0) 
	return NULL;

    if (this->size >= SD_STACK_INIT_SIZE * 4 && this->sp < this->size / 4) {
	size_t new_size = this->size / 2;

	this->size = new_size;
	this->array = (void **)sd_realloc(this->array, sizeof(*this->array) * this->size);
    }
    if(this->sp < 0 && this->sp <= this->size){
		printf("this->sp < 0 or   this->sp <= this->size");
    }
    assert(this->sp > 0 && this->sp <= this->size);
    return this->array[--(this->sp)];
}

int main(void)
{
	char ptr[] = "hello";
	int i = 0;
	char * pop;
	sd_stack_t *new_stack = NULL;
	new_stack = sd_stack_new(0);
	printf("max = %d  sp = %d  size = %d itlr = %d\n", new_stack ->max, new_stack ->sp, new_stack ->size, new_stack ->iter);
	for(i = 0; i < strlen(ptr); i++){
		sd_stack_push(new_stack, (ptr+i));
		printf("max = %d  sp = %d  size = %d  itlr = %d\n", new_stack ->max, new_stack ->sp, new_stack ->size, new_stack ->iter);
	}
	#if 1
	for(i = 0; i < new_stack ->sp; i++){
		pop = sd_stack_pop(new_stack);
		printf("%c\n", *pop);
		printf("max = %d  sp = %d  size = %d  itlr = %d\n", new_stack ->max, new_stack ->sp, new_stack ->size, new_stack ->iter);
	}
	#endif
#if 0
	pop = sd_stack_pop(new_stack);
	pop = sd_stack_pop(new_stack);
	pop = sd_stack_pop(new_stack);
	pop = sd_stack_pop(new_stack);
	pop = sd_stack_pop(new_stack);
#endif
	return 0;
}
